package com.linln.admin.bank.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.admin.bank.domain.Clue;
import com.linln.admin.bank.repository.ClueRepository;
import com.linln.admin.bank.service.ClueService;
import com.linln.admin.core.enums.StatusEnum;

/**
 * @author kome
 * @date 2020/08/08
 */
@Service
public class ClueServiceImpl implements ClueService {

    @Autowired
    private ClueRepository clueRepository;

    /**
     * 根据业务管理ID查询业务管理数据
     * 
     * @param id 业务管理ID
     */
    @Override
    @Transactional
    public Clue getId(Long id) {
        Byte[] status = { StatusEnum.OK.getCode(), StatusEnum.FREEZED.getCode() };
        return clueRepository.findByIdAndStatusIn(id, status);
    }

    /**
     * 获取分页列表数据
     * 
     * @param example 查询实例
     * @param pageIndex 页码
     * @param pageSize 获取列表长度
     * @return 返回分页数据
     */
    @Override
    public Page<Clue> getPageList(Example<Clue> example, Integer pageIndex, Integer pageSize) {
        // 创建分页对象
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest page = PageRequest.of(pageIndex - 1, pageSize, sort);
        Page<Clue> list = clueRepository.findAll(example, page);
        return list;
    }

    /**
     * 保存业务管理
     * 
     * @param clue 业务管理实体类
     */
    @Override
    public Clue save(Clue clue) {
        return clueRepository.save(clue);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Integer updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return clueRepository.updateStatus(statusEnum.getCode(), idList);
    }

    @Override
    @Transactional
    public Integer updateCompleteStatus(int status, List<Long> idList) {
        return clueRepository.updateCompleteStatus(status, idList);
    }
}
